package com.cruise.system.userhandle.controller;

import com.cruise.common.web.result.Result;
import com.cruise.system.userapi.dto.RegistUserDTO;
import com.cruise.system.userapi.dto.UpdateUserDataCountDTO;
import com.cruise.system.userapi.entity.SysRole;
import com.cruise.system.userapi.entity.SysUser;
import com.cruise.system.userapi.feignapi.FeignSystemApi;
import com.cruise.system.userhandle.service.SysRoleService;
import com.cruise.system.userhandle.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类名：SystemController
 * 描述：TODO
 * @author cruise
 * 日期：2021/1/16 下午11:07
 * 版本：1.0
 **/

@Api(tags = "系统接口")
@RequestMapping("/sysuser")
@RestController
public class SystemController implements FeignSystemApi {


    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation("根据用户名查询用户信息")
    @Override
    @GetMapping("/findByUsername")
    public Result<SysUser> findByUsername(String username) {


        return Result.success(sysUserService.findByUsername(username));
    }

    @ApiOperation("根据用户ID查询用户的角色")
    @Override
    @GetMapping("/getRoleByUserId")
    public Result<List<SysRole>> getRoleByUserId(String id) {
        return Result.success(sysRoleService.getRoleByUserId(id));
    }

    @Override
    @GetMapping("/findByPhone")
    public Result<SysUser> findByPhone(String phone) {

        return Result.success(sysUserService.findByPhone(phone));
    }

    /**
     * 用户注册 角色默认是 普通用户
     * @param userDto
     * @return
     */
    @ApiOperation(value = "用户注册接口")
    @PostMapping("/regist")
    @Override
    public Result<String> regist(@RequestBody RegistUserDTO userDto){

        SysUser user = new SysUser();
        BeanUtils.copyProperties(userDto, user);
        String regist = sysUserService.regist(user);
        return Result.success(regist);
    }


    /**
     * 根据ID查询用户
     * @param userId
     * @return
     */
    @Override
    @PostMapping("/findById")
    public SysUser findById(String userId) {
        SysUser sysUser = sysUserService.getById(userId);
        return sysUser;
    }

    /**
     * 查询手机号是否存在
     * @param phoneNumber
     * @return
     */
    @Override
    @PostMapping("/checkPhoneAvailable")
    public Boolean checkPhoneAvailable(String phoneNumber) {
        return sysUserService.checkPhoneAvailable(phoneNumber);
    }

    /**
     * 更新统计数据
     * @param updateUserDataCountDTO
     * @return
     */
    @Override
    @PostMapping("/updateUserDataCount")
    public Result updateUserDataCount(@RequestBody UpdateUserDataCountDTO updateUserDataCountDTO){

        sysUserService.updateUserDataCount(updateUserDataCountDTO);
        return  Result.success();
    }

}
